<?php

class Siteusers extends CI_Model {

	public function __construct()
	{
		$this->load->database();
	}
	
	public function get_all_users(){
		//$this->input->post('title')
		$query = $this->db->query("SELECT a.*, b.*, c.* 
								FROM users_table a, user_info b, cities c 
								WHERE a.user_id = b.user_id AND a.type_id = 2 AND b.city_id = c.city_id 
								ORDER BY b.form_number desc ");
		return $query->result_array();
		
		//echo "usermodel";
	}
	
	public function change_status(){
		$data = array(
			'user_status' => $this->input->post('status_val')
		);
		//$query = "UPDATE users_table SET user_status = '".."' WHERE user_id = '".$this->input->post('userId')."' ";
		$this->db->update('users_table', $data, array('user_id' => $this->input->post('userId')));
		
		$user_details = $this->get_user_details($this->input->post('userId'));
		
		/*
		$user_package = substr($user_details['reg_number'],0,1);
		
		if($user_package == 'B'){
			$point4trans = 2000;
			
		}
		elseif($user_package == 'S'){
			$point4trans = 4000;
		}
		elseif($user_package == 'G'){
			$point4trans = 6000;
		}
		elseif($user_package == 'P'){
			$point4trans = 10000;
		}
		*/
		$level_id = $user_details['level_id'];
		
		if($level_id == 1){
			$point4trans = 2000;
		}
		elseif($level_id == 2){
			$point4trans = 4000;
		}
		elseif($level_id == 3){
			$point4trans = 6000;
		}
		elseif($level_id == 4){
			$point4trans = 10000;
		}
		elseif($level_id == 5){
			$point4trans = 1000;
		}
		if($this->input->post('status_val') == 2){
			$the_msg = "Your account is suspended by site administrator.";
			$msg_qry = "INSERT INTO message_table
						(`msg_datetime`, `msg_from` , `msg_to` ,`msg_subject`, `msg_text` , `reply_of`, `msg_status` , `msg_type`) VALUES
						('".date('Y-m-d H:i:s')."', '1', '".$this->input->post('userId')."', 'Your Account is suspended.', '".$the_msg."' , '0', '0','0')";
			$msg_res = $this->db->query($msg_qry);
			
			echo '1';
			return 1;
		}
		else{
			$points_qry = "INSERT INTO point_trans
							(`point_trans_datetime`, `in_user_id`, `out_user_id`, `point_trans_val`, `point_trans_details`) VALUES
							('".date('Y-m-d H:i:s')."', '".$this->input->post('userId')."' ,'1', '".$point4trans."', 'Your account is acctivated by transfering ".$point4trans."  points into your account.')";
			$query = $this->db->query($points_qry);
			
			$info_query = "UPDATE user_info SET curr_points = '0' WHERE user_id = '".$this->input->post('userId')."' ";
			$query = $this->db->query($info_query);
			
			$the_msg = "Congratulation ! Your account is activated.";
			$msg_qry = "INSERT INTO message_table
						(`msg_datetime`, `msg_from` , `msg_to` ,`msg_subject`, `msg_text` , `reply_of`, `msg_status` , `msg_type`) VALUES
						('".date('Y-m-d H:i:s')."', '1', '".$this->input->post('userId')."', 'Your Account is activated.', '".$the_msg."' , '0', '0','0')";
			$msg_res = $this->db->query($msg_qry);
			
			echo '2';
			return 0;
		}
	}
	
	public function get_user_details($the_userid){
		$query = $this->db->query("SELECT a.*, b.*, c.qualif_title, d.country_name, e.city_name , f.marital_title 
								FROM users_table a, user_info b, qualifications c, countries d, cities e, marital_table f
								WHERE a.user_id = b.user_id AND a.type_id = 2 AND a.user_id ='".$the_userid."' AND b.qalification = c.qualif_id
								AND b.country_id = d.country_id AND b.city_id = e.city_id AND b.maritalstatus = f.marital_id  ");
		return $query->row_array();
	}
	
	public function new_user($user_pic){
		//$this->input->post('title')
		$fullname 	= $this->input->post('fullname');
		$useremail 	= $this->input->post('useremail');
		$pwd 		= $this->input->post('pwd');
		$user_package= $this->input->post('user_package');
		
		$dob_d 		= $this->input->post('dob_d');
		$dob_m 		= $this->input->post('dob_m');
		$dob_y		= $this->input->post('dob_y');
		
		$the_dob = $dob_y."-".$dob_m."-".$dob_d;
		
		$user_gender 		= $this->input->post('user_gender');
		$maritalstatus 		= $this->input->post('maritalstatus');
		$blood_group 		= $this->input->post('blood_group');
		$qualifications 	= $this->input->post('qualifications');
		$mail_address 		= $this->input->post('mail_address');
		$country			= $this->input->post('country');
		$city			 	= $this->input->post('city');
		//$cnic			 	= $this->input->post('cnic');
		$cnic			 	= $this->input->post('cnic_1').'-'.$this->input->post('cnic_2').'-'.$this->input->post('cnic_3');
		$mobile			 	= $this->input->post('mobile');
		$sec_ques		 	= $this->input->post('sec_ques');
		$sec_ans		 	= $this->input->post('sec_ans');
		$reg_form_no	 	= $this->input->post('reg_form_no');
		$ref_id			 	= $this->input->post('ref_id');
		
		$query = $this->db->query("SELECT * FROM users_table WHERE login_email = '".$useremail."' ");
		$res = $query->result_array();
		if(empty($res)){
			//	Get the reg_number
			$user_number = $this->get_user_number($user_package);
			
			$user_qry = "INSERT INTO `users_table` (`login_email`, `login_pass`, `type_id`, `user_status`) VALUES
								( '".$useremail."', '".$pwd."', 2, 0) ";
			
			$user_res = $this->db->query($user_qry);
			
			$new_user_id = $this->db->insert_id();
			//echo "new_user_id=".$new_user_id;
			//exit;
			if($user_package == 'ST'){
				$level_id = 5;
				$point4trans = 1000;
				$point4info = -1000;
				
			}
			elseif($user_package == 'B'){
				$level_id = 1;
				$point4trans = 2000;
				$point4info = -2000;
				
			}
			elseif($user_package == 'S'){
				$level_id = 2;
				$point4trans = 4000;
				$point4info = -4000;
			}
			elseif($user_package == 'G'){
				$level_id = 3;
				$point4trans = 6000;
				$point4info = -6000;
			}
			elseif($user_package == 'P'){
				$level_id = 4;
				$point4trans = 10000;
				$point4info = -10000;
			}
			
			//////////////	Entry in info table
			$info_qry = "INSERT INTO `user_info` 
				(`user_id`, `reg_number`, `level_id`, `fullname`, `dateofbirth`, `gender`, `maritalstatus`,`pic_path`, `bloodgroup`, 
				`qalification`, `mailingaddress`, `city_id`, `country_id`, `cnic`, `mobilenumber`, `sec_question`, `sec_answer`, `form_number`, 
				`ref_code`, `created_on`, `modified_on`, `status`, `activated_on`, `curr_points`) 
			VALUES 
				('".$new_user_id."', '".$user_number."', '".$level_id."', '".$fullname."', '".$the_dob."', '".$user_gender."', '".$maritalstatus."', '".$user_pic."', '".$blood_group."', 
				'".$qualifications."', '".$mail_address."', '".$city."', '".$country."', '".$cnic."', '".$mobile."', '".$sec_ques."', '".$sec_ans."', '".$reg_form_no."', 
				'".$ref_id."', '".date('Y-m-d H:i:s')."', '".date('Y-m-d H:i:s')."', '0', '','".$point4info."');";
			
			$info_res = $this->db->query($info_qry);
			
			
			//////////////////	Entry in points table
			$points_qry = "INSERT INTO point_trans
							(`point_trans_datetime`, `in_user_id`, `out_user_id`, `point_trans_val`, `point_trans_details`) VALUES
							('".date('Y-m-d H:i:s')."', '1', '".$new_user_id."' , '".$point4trans."', 'New user created with ".$point4info."  points')";
			
			$points_res = $this->db->query($points_qry);
			
			/////////////////////	Entry in messages
			$the_msg = "Congratulation ! Your account with waastapk.com is created. This account will be activated after confimation of your details in 24 hours.
						In case of delay you can contact the site administrator by replying this message. Thanks.";
			$msg_qry = "INSERT INTO message_table
						(`msg_datetime`, `msg_from` , `msg_to` ,`msg_subject`, `msg_text` , `reply_of`, `msg_status` , `msg_type`) VALUES
						('".date('Y-m-d H:i:s')."', '1', '".$new_user_id."', 'New Account created', '".$the_msg."' , '0', '0','0')";
			$msg_res = $this->db->query($msg_qry);
			
			//return 1;
			return $user_number;
		}
		else{
			//return "The Email already exists";
			return '0';
		}
	}
	
	public function get_user_number($pack_code){
		$srch_str = $pack_code.date('Y').date('m').date('d');
		$query = $this->db->query("SELECT reg_number FROM user_info WHERE reg_number like '%".$srch_str."%' ");
		$res = $query->result_array();
		if(empty($res)){
			return $srch_str."1";
		}
		else{
			//print_r($res);
			//exit;
			//foreach($res as $val){
				
			//}
			$counter = 1;
			$test_val = $srch_str.$counter;
			$testing_array = array();
			foreach($res as $theval){
				//echo $theval['reg_number'];	
				array_push($testing_array, $theval['reg_number']);
			}
			
			while($counter <= 100){
				$test_val = $srch_str.$counter;
				
				if(in_array($test_val , $testing_array)){
					$counter++;
					//break;
				}
				else{
					break;
				}
				
				
			}
		}
		//exit;
		return $test_val;
	}
	
	public function get_user_tree($ref_id){
		$query = $this->db->query("SELECT * FROM user_info WHERE ref_code = '".$ref_id."' ");
		$res = $query->result_array();
		
		return $res;
	}
	
	public function get_child_count($reg_num){
		$the_count = 0;
		
		$query = $this->db->query("SELECT count(*) as child_count FROM user_info WHERE ref_code = '".$reg_num."' ");
		$res = $query->row_array();
		
		if(!empty($res)){
			$the_count = $res['child_count'];
		}
		
		echo $the_count;
	}
	
	public function get_user_trans_history($user_id){
		$trans_qry = "SELECT a.point_trans_id, a.point_trans_datetime, a.in_user_id, a.out_user_id, a.point_trans_val as debit_vsl, 0 as credit_val, 
								a.point_trans_details, b.user_id as the_user_id, b.reg_number, b.fullname, 'in' as trans_type 
						FROM point_trans a, user_info b 
						WHERE a.out_user_id = '".$user_id."' AND a.in_user_id = b.user_id 
						UNION
						SELECT a.point_trans_id, a.point_trans_datetime, a.in_user_id, a.out_user_id, 0 as debit_vsl, a.point_trans_val as credit_val, 
								a.point_trans_details, b.user_id as the_user_id, b.reg_number, b.fullname, 'out' as trans_type 
						FROM point_trans a, user_info b 
						WHERE a.in_user_id = '".$user_id."' AND a.out_user_id = b.user_id ";
		
		$query = $this->db->query($trans_qry);
		$res = $query->result_array();
		
		return $res;
	}
	
	public function get_form_numbers(){
		$list_qry = "SELECT * FROM forms_number_listing ORDER BY form_list_datatime DESC";
		
		$query = $this->db->query($list_qry);
		$res = $query->result_array();
		
		return $res;
	}
	
	public function get_form_data($formid){
		$list_qry = "SELECT * FROM forms_number_listing  WHERE form_list_id = '".$formid."' ";
		
		$query = $this->db->query($list_qry);
		$res = $query->row_array();
		
		return $res;
	}
	
	public function save_form_number(){
		$cnic_number = $this->input->post('cnic_1').'-'.$this->input->post('cnic_2').'-'.$this->input->post('cnic_3');
		$forms_data = array(
			'form_number' 		=> $this->input->post('form_number'),
			'cnic'				=> $cnic_number,
			'form_list_datatime'=> date('Y-m-d H:i:s'),
			'form_list_status'	=> $this->input->post('form_status')
		);
		
		$query = $this->db->insert_string('forms_number_listing', $forms_data);
		$this->db->query($query);
	}
}

?>